package com.yoipdu.im1.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Random;

public class Tools {
    private final String SYMBOLS = "0123456789";
    private Random random = new Random();

    public String md5(String str) {
        str += "yoipdu";
        String md5Code = "";
        if (str != null) {
            try {
                //md5实现类实例化
                byte[] ret = MessageDigest.getInstance("md5").digest(str.getBytes());
                //将获取的byte数组值转为16进制的字符串
                md5Code = new BigInteger(1, ret).toString(16);
                //获取的字符串不足32位的用“0”补齐
                for (int i = 0; i < 32 - md5Code.length(); i++) {
                    md5Code = "0" + md5Code;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return md5Code;
    }

    public String generateVerCode() {
        char[] nonceChars = new char[6];
        for (int index = 0; index < nonceChars.length; index++) {
            nonceChars[index] = SYMBOLS.charAt(random.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }
}
